﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;
using NotebookS2.Sales.Dialogs; 

namespace NotebookS2
{
    public partial class FND_ITM_STCKRQ : RibbonForm
    {
        public FND_ITM_STCKRQ()
        {
            InitializeComponent();
        }

        private string _searchedItem = "";

        public string SearchedItem
        {
            get { return _searchedItem; }
            set { _searchedItem = value; }
        }
        public string SelectedItemCode { get; set; }
        public string SelectedItemDescription { get; set; }
        public string SelectedUoM { get; set; }

        public string ConnectionString { get; set; }

        void LoadByCode(string sSearch)
        {
            ItemManager oManager = new ItemManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = oManager.BrowseItemMasterDataByCode(sSearch);
            oManager.Close();

            gridControl1.DataSource = oTable;
            gridControl1.Refresh();
        }

        void LoadByName(string sSearch)
        {
            ItemManager oManager = new ItemManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = oManager.BrowseItemMasterDataByName(sSearch);
            oManager.Close();

            gridControl1.DataSource = oTable;
            gridControl1.Refresh();
        }

        private void FND_ITM_STCKRQ_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 1;
            txtSearch.Text = this.SearchedItem;

            txtSearch_ButtonClick(sender, null);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            int[] iRow = gridView1.GetSelectedRows();

            int iRowNum = iRow[0];

            this.SelectedItemCode = gridView1.GetRowCellValue(iRowNum, gridView1.Columns["ItemCode"]).ToString();
            this.SelectedItemDescription = gridView1.GetRowCellValue(iRowNum, gridView1.Columns["Description"]).ToString();
            this.SelectedUoM = gridView1.GetRowCellValue(iRowNum, gridView1.Columns["InvtyUntMsr"]).ToString();

            this.Close();
        }


        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm5));
          
            string sSearch = string.Format("%{0}%", txtSearch.Text);

            if (cboSearch.SelectedIndex == 1)
            {
                LoadByCode(sSearch);
            }
            else
            {
                LoadByName(sSearch);
            }

            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();

        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }
    }
}
